package com.rtx.edi.platform.express.config.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.api.annotation.RtxExpressApi;
import com.rtx.api.enums.FieldValidateType;
import com.rtx.api.utils.FieldValidateUtils;
import com.rtx.edi.model.express.po.PlExpressApiConfig;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.express.config.service.PlExpressApiConfigService;
import com.rtx.express.service.IExpressService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Service;

@SuppressWarnings("all")
@Api(tags = {"快递配置信息"})
@RestController
@RequestMapping("/v1/PlExpressApiConfig")
public class PlExpressApiConfigController {
	
	@Autowired
	PlExpressApiConfigService configService;
	
	@Autowired
	private Map<String,IExpressService> expressServices;
	
	@ApiOperation(value = "获取快递接口API", notes = "获取快递接口API")
	@PostMapping("selectExpressApiSelect")
	public EdiResponse selectExpressConfig(@RequestBody(required = false) String request) {
		
		List<Map<String,String>> select =new ArrayList<>();

		Map<String,String> nulls =new HashMap<String, String>();
		nulls.put("label", "");
		nulls.put("value", "");
		select.add(nulls);

		for(String name:expressServices.keySet()) {
			Class clazz = expressServices.get(name).getClass();
			Service se = (Service) clazz.getAnnotation(Service.class);
			RtxExpressApi api = (RtxExpressApi) clazz.getAnnotation(RtxExpressApi.class);
			if(null== api) {
				continue;
			}
			Map<String,String> stdjob =new HashMap<String, String>();
			stdjob.put("label", api.expressApiName());
			stdjob.put("value", se.value());
			select.add(stdjob);
		}


		return EdiResponse.success().data("item", select);
	}
	
    /**
     * 获取快递配置信息列表
     * @return
     */
    @ApiOperation(value = "快递配置信息列表", notes = "分页获取快递配置信息列表")
    @PostMapping("selectExpressApiConfigPageList")
    public EdiResponse selectExpressApiConfigPageList(@RequestBody PlExpressApiConfig dto){
        Page<PlExpressApiConfig> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
        configService.selectList(pageParam,dto);
        return EdiResponse.success().data("page",pageParam);
    }

    /**
     * 获取快递配置信息
     * @return
     */
    @ApiOperation(value = "获取快递配置信息", notes = "根据ID获取快递配置信息")
    @PostMapping("selectExpressApiConfigById")
    public EdiResponse selectExpressApiConfigById(@RequestBody PlExpressApiConfig dto){
        return EdiResponse.success().data("item",configService.getById(dto.getId()));
    }

    /**
     * 新增快递配置信息
     * @return
     */
    @ApiOperation(value = "新增快递配置信息", notes = "新增快递配置信息")
    @PostMapping("addExpressApiConfig")
    public EdiResponse addExpressApiConfig(@RequestBody PlExpressApiConfig dto){
        //验证非空字段
        configService.addExpressApiConfig("ris_edi", dto);
        return  EdiResponse.success() ;
    }

    /**
     * 修改快递配置信息
     * @return
     */
    @ApiOperation(value = "修改快递配置信息", notes = "根据ID修改快递配置信息")
    @PostMapping("editExpressApiConfig")
    public EdiResponse editExpressApiConfig(@RequestBody PlExpressApiConfig dto){
    	configService.updateById(dto);
        return  EdiResponse.success();
    }

    /**
     * 删除快递配置信息
     * @return
     */
    @ApiOperation(value = "删除快递配置信息", notes = "根据ID删除快递配置信息")
    @PostMapping("deleteExpressApiConfig")
    public EdiResponse deletePlApplicationFieldById(@RequestBody PlExpressApiConfig dto){
        FieldValidateUtils.fieldNotNull(dto,FieldValidateType.DELETE);
        return  configService.removeById(dto.getId()) ? EdiResponse.success() : EdiResponse.failed();
    }

    /**
     * 批量删除快递配置信息
     * @return
     */
    @ApiOperation(value = "批量删除快递配置信息", notes = "批量删除快递配置信息")
    @PostMapping("deleteExpressApiConfigIds")
    public EdiResponse deletePlApplicationFieldByIds(@RequestBody List<PlExpressApiConfig> list){
        List<Long> ids = list.stream().map(p -> p.getId()).distinct().collect(Collectors.toList());
        return  configService.removeBatchByIds(ids) ? EdiResponse.success() : EdiResponse.failed();
    }
	

}
